package com.ken.astroboy.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ken.astroboy.model.AutoReplyEntity;
import com.ken.astroboy.model.Contact;
import com.ken.astroboy.model.ScheduledTaskEntity;
import com.ken.astroboy.model.UserGroupCtrlEntity;
import com.ken.astroboy.model.WelcomeMsgEntity;

public class InMemoryStorage {
	
	private static final Logger logger = LoggerFactory.getLogger(InMemoryStorage.class);
	
	//保存每个群动态加入的成员，用于发送欢迎消息，欢迎消息发送后，清空该Map
	public static Map<String, List<String>> newMemberMap = new HashMap<String, List<String>>();
	
	//保存微信群信息
	public static Map<String, Contact> groupMap = new HashMap<String, Contact>();
	//保存好友信息
	public static Map<String, Contact> friendMap = new HashMap<String, Contact>();
	//保存公众号信息
	public static Map<String, Contact> mpMap = new HashMap<String, Contact>();
	
	//机器人控制的群列表
	//public static List<String> groupUnderCtrlList = new ArrayList<String>();
	//管理员列表
	//public static List<String> adminlList = new ArrayList<String>();
	//用于特殊处理的用户列表
	//public static List<String> userFilteringList = new ArrayList<String>();
	
	//用userGroupCtrlEntity代替上面的三个List
	public static UserGroupCtrlEntity userGroupCtrlEntity = new UserGroupCtrlEntity();

	public static WelcomeMsgEntity welcomeMsgEntity = new WelcomeMsgEntity();
	
	public static AutoReplyEntity autoReplyEntity = new AutoReplyEntity();
	
	public static ScheduledTaskEntity scheduledTaskEntity = new ScheduledTaskEntity();
	
	//保存KeepUp互助减肥挑战群
	public static List<String> gymGroupList = new ArrayList<>();
	
	public static boolean allowFriendAutoReply = false;
	
	public static boolean needToReloadQuartzAndStartScheduler = false;
	
	public static boolean isRobotOnline = false;

	public static boolean isRobotInitializing = false;
	
	public static boolean isRobotHealthy = false;
	
	public static boolean keepListening = false;


	
	public static String findGroupNickName(String groupUserName){
		
		if(groupUserName == null){
			return null;
		}
		
		String groupNickName = null;
		
		for(Map.Entry<String, Contact> entry: groupMap.entrySet()){
		
			if(entry.getValue() != null){
				if(groupUserName.equals(entry.getValue().getUserName())){
					groupNickName = entry.getKey();
					break;
				}				
			}
		}	
		
		return groupNickName;		
	}

	public static String findGroupUserName(String groupNickName){
		
		if(groupNickName == null){
			return null;
		}
		
		String groupUserName = null;
				
		for(Map.Entry<String, Contact> entry: groupMap.entrySet()){
		
			if(entry.getValue() != null){
				if(groupNickName.equals(entry.getKey())){
					groupUserName = entry.getValue().getUserName();
					break;
				}				
			}
		}	
		
		return groupUserName;		
	}
	
	public static String findSenderNickName(String groupNickName, String senderUserName){
	
		if(senderUserName == null || groupNickName == null){
			return null;
		}
		
		String senderNickName = null;
		
		Contact contact = groupMap.get(groupNickName);
		
		if(contact.getMemberList() != null){
			for(Contact member: contact.getMemberList()){
				if(senderUserName.equals(member.getUserName())){
					senderNickName = member.getNickName();
					break;
				}
			}			
		}
		
		return senderNickName;		
	}

	
	public static String findSenderNickName(String senderUserName){
		
		if(senderUserName == null){
			return null;
		}
		
		String senderNickName = null;
		
		for(Map.Entry<String, Contact> friend : friendMap.entrySet()){
			if(senderUserName.equals(friend.getValue().getUserName())){
				senderNickName = friend.getKey();
				break;
			}
		}		
		return senderNickName;		
	}
	
	
	public static long findSenderAttrStatusName(String groupNickName, String senderUserName){
		
		if(senderUserName == null || groupNickName == null){
			return -1;
		}
		
		long senderAttrStatus = -1;
		
		Contact contact = groupMap.get(groupNickName);
		
		if(contact.getMemberList() != null){
			for(Contact member: contact.getMemberList()){
				if(senderUserName.equals(member.getUserName())){
					senderAttrStatus = member.getAttrStatus();
					break;
				}
			}			
		}
		
		return senderAttrStatus;		
	}
	
	public static void printStorageInfo(){
		
		for(Map.Entry<String, Contact> entry: groupMap.entrySet()){
			logger.info("微信群:[{}],成员数:[{}]", entry.getKey(), entry.getValue().getMemberCount());
		}

		for(Map.Entry<String, Contact> entry: friendMap.entrySet()){
			logger.info("好友:[{}], Username:[{}]", entry.getKey(), entry.getValue().getUserName());
		}
		
		for(Map.Entry<String, Contact> entry: mpMap.entrySet()){
			logger.info("关注公众号:[{}], Username:[{}]", entry.getKey(), entry.getValue().getUserName());
		}
		
		for(String entry: userGroupCtrlEntity.getGroupList()){
			logger.info("被收编的微信群:[{}]", entry);
		}
		
		for(String entry: userGroupCtrlEntity.getSignInGroupList()){
			logger.info("开启打卡功能的微信群:[{}]", entry);
		}		
	
		for(String entry: userGroupCtrlEntity.getAdminList()){
			logger.info("管理员:[{}]", entry);
		}
		
		for(String entry: userGroupCtrlEntity.getNoticeReceiverList()){
			logger.info("接收通知用户:[{}]", entry);
		}
		
		
	}
	
	public static void init(){

	    newMemberMap = new HashMap<String, List<String>>();	    
	    groupMap = new HashMap<String, Contact>();
	    friendMap = new HashMap<String, Contact>();
	    mpMap = new HashMap<String, Contact>();
	    userGroupCtrlEntity = new UserGroupCtrlEntity();
	    welcomeMsgEntity = new WelcomeMsgEntity();
	    autoReplyEntity = new AutoReplyEntity();
	    scheduledTaskEntity = new ScheduledTaskEntity();
	    gymGroupList = new ArrayList<>();
	    allowFriendAutoReply = false;
	    needToReloadQuartzAndStartScheduler = false;
	    isRobotOnline = false;
	    isRobotInitializing = false;
	    isRobotHealthy = false;
	    keepListening = false;
	}
}
